package com.carlinfo.hospital.util;

import org.springframework.test.annotation.SystemProfileValueSource;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ReflectUtil
{
    public static Object[] zh(Object o)
    {
        List<Object> li  = new ArrayList<Object>();
        Class c = o.getClass();
        Field[] declaredFields = c.getDeclaredFields();
        for (Field field : declaredFields)
        {
            if(field.getName().equalsIgnoreCase("id"))
            {
                continue;
            }
            String type = field.getType().getName();
            if(type.equalsIgnoreCase(Integer.class.getName())
                    ||type.equalsIgnoreCase("double")
                    ||type.equalsIgnoreCase("byte")
                    ||type.equalsIgnoreCase("boolean")
                    ||type.equalsIgnoreCase(Date.class.getName())
                    ||type.equalsIgnoreCase(String.class.getName()))
            {
                String str = "get"+field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
                try
                {
                    Method method = c.getMethod(str);
                    Object name = method.invoke(o);
                    li.add(name);
                } catch (NoSuchMethodException e)
                {
                    e.printStackTrace();
                } catch (IllegalAccessException e)
                {
                    e.printStackTrace();
                } catch (InvocationTargetException e)
                {
                    e.printStackTrace();
                }
            }
        }
        Object[] objects  =new Object[li.size()];
        li.toArray(objects);
        return objects;
    }
}
